import XYRTC from "@/utils/xy/xy-mp-sdk";

const { miniProgram } = uni.getAccountInfoSync();
const XYClient = XYRTC.createClient({
  extId: process.env.NODE_ENV === 'production' && miniProgram.appId === 'wx238ad63939c8de76' ? '1e4bbc85ae104cdae67047a69e10a7b10c28339f' : '5986b1f807f8c978a2f52e1473715e85fc07e8fc', 
  appId: process.env.NODE_ENV === 'production' && miniProgram.appId === 'wx238ad63939c8de76' ? 'lFJHJbzBlEVrpsvD7AqQUZjZ' : 'WvJ8vQb4sJWs36zdZt8bP1F6'
});
import userApi from "@/api/user.js";

const loginCB = function (res) {
    if(res.code === 200 || res.code === "XYSDK:980200"){ // 状态是200时，初始化登录成功
        console.log("小鱼登录成功: ", res);
    }else{
        uni.showToast({
            title: '小鱼登录失败',
            icon: 'none',
            complete: () => {
                setTimeout(() => {
                    uni.navigateBack()
                }, 1500);
            }
        })
    }
}

const state = {
    cellInfo: uni.getStorageSync("xy_cellInfo") || {}, // 拨打会议信息
}

const mutations = {
    // 设置会议信息
    SET_CELLINFO(state, data){
        state.cellInfo = data
        uni.setStorageSync("xy_cellInfo", state.cellInfo);
    },
}

const actions = {
    /**
     * 登录小鱼
    */
     loginXy({ state }) {
      XYClient.loginExternalAccount({
        displayName: state.userInfo.userName,
        extUserId: state.userInfo.id,
      }).then(res=>loginCB(res))

        // if(state.userInfo.token){
        //     XYClient.login(state.userInfo.xiaoyuidToken, loginCB);
        // }
    },
    // 上报用户状态
    reportStatus({ state }, params) {
      if(params.orderNo && params.orderNo == '000000000'){
        delete params.orderNo
      }
      userApi.escalation(params)
    }

}

export default {
    state,
    mutations,
    actions
}